global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


use "W:\intimate\data\allcouple_data_clean", clear
keep shnro sphnro0 ptoim1* tyotu* seutukunta16* age educ male year_start_cohab
g person1 = shnro
g person2 = sphnro0
drop shnro sphnro0
ren person2 shnro
ren person1 sphnro0 

foreach thing in ptoim1 tyotu seutukunta16 {
    ren `thing'0 part_`thing'0
	forvalues i = 1/5 {
	    ren `thing'B`i' part_`thing'B`i'
		ren `thing'F`i' part_`thing'F`i'
	}
}
foreach thing in age educ male {
    ren `thing' part_`thing'
}
duplicates drop
tempfile partdata
save `partdata'

use "W:\intimate\data\match_victim_data_clean_new", clear
merge m:1 shnro sphnro0 year_start_cohab using `partdata'
keep if _merge==3
drop _merge


// change into long format
local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse seutukunta16 kunta part_seutukunta16 part_ptoim1 part_tyotu
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n

reshape long `varlist', i(group_id) j(time)


// merge in outside options
rename educ level 
gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6 
 
ren part_educ part_level 
g part_educ=1 if part_level<3
replace part_educ=2 if part_leve>=3 & part_level<=5
replace part_educ=3 if part_level>=6

replace age=floor(age/5)
replace part_age = floor(part_age/5)

g male =0

drop year_event year
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1
g time_ = time-6

merge m:1 year educ seutukunta16 age male using "$data_folder\munic_edu_age_gender_oo", keepusing(emp obs ann_earn)
drop if _merge==2 
drop _merge 

gen emp_rate_lo=(emp-ptoim1)/(obs-1)
gen wage_lo=(ann_earn-tyotu)/(obs-1)
foreach var in seutukunta16 educ age male emp_rate_lo wage_lo {
    rename `var' own_`var'
}
drop emp obs ann_earn

rename part_educ educ 
rename part_seutukunta16 seutukunta16
g male = 1

ren part_age age

merge m:1 year educ seutukunta16 age male using "$data_folder\munic_edu_age_gender_oo", keepusing(emp obs ann_earn)
drop if _merge==2 
drop _merge 


gen emp_rate_lo=(emp-part_ptoim1)/(obs-1)
gen wage_lo=(ann_earn-part_tyotu)/(obs-1)

foreach var in seutukunta16 educ age emp_rate_lo wage_lo {
    rename `var' part_`var'
	ren own_`var' `var'
}

replace same_spouse=0 if same_spouse==. 


*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
egen munic_id = group(seutukunta16)

global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

cap drop breakup 
gen breakup=1-same_spouse

gen emp_rate_lo_dv=emp_rate_lo*dv_couple 
gen part_emp_rate_lo_dv=part_emp_rate_lo*dv_couple 

replace breakup=0 if time<=5 

sum emp_rate_lo if male==0 & dv_couple==0, d 
sum emp_rate_lo if male==1 & dv_couple==0, d 

label var emp_rate_lo "Emp F"
label var part_emp_rate_lo "Emp M"
label var emp_rate_lo_dv "Emp F x DV"
label var part_emp_rate_lo_dv "Emp M x DV"
label var dv_couple "DV"


reghdfe breakup dv_couple emp_rate_lo  part_emp_rate_lo emp_rate_lo_dv part_emp_rate_lo_dv,   absorb($fe)  cluster($cluster)
su breakup if dv_couple==0 



